import { urlParams } from "./index";
import dayjs from "dayjs";

const userInfo = {
  data() {
    return {
      userInfo: {}
    };
  },
  computed: {},
  methods: {
    async getUserInfo() {
      const res = await this.server.get("/common/findUserByUserAccount", { userAccount: urlParams.get("userAccount") });
      if (res.code === "0") this.userInfo = res.object;
    }
  }
};


const form = {
  data() {
    return {
      loadingSubmit: false,
      currentKey: "",
      currentForm: null,
      currentOptions: [],
      currentOptionsCache: [],
      showSearchMaxOptionsLength: 7,

      currentDate: new Date(),
      showDatetimePicker: false, // 时间选择器
      showCalendar: false, // 日期选择器
      dateFormat: '',
      showPicker: false, // 下拉选择器
      showPickerMul: false, // 下拉多选
      pickerKeyword: '',

      checkBoxMul: [], // 多选选中

      callback: null,

      minDate: new Date(2010, 0, 1),
      maxDate: new Date()
    };
  },
  methods: {

    openDatetimePicker(key, data, format) {
      this.currentKey = key;
      this.currentForm = data || this.form;
      this.dateFormat = format || 'YYYY-MM-DD HH:mm'
      this.showDatetimePicker = true;
    },
    openCalendar(key, data, format) {
      this.currentKey = key;
      this.currentForm = data || this.form;
      this.dateFormat = format || 'YYYY-MM-DD'
      this.showCalendar = true;
    },
    openPicker(key, data, callback) {
      this.pickerKeyword = ''
      this.callback = callback
      this.currentKey = key;
      this.currentForm = data || this.form;
      this.currentOptions = this.options[key];
      this.currentOptionsCache = this.options[key];
      this.showPicker = true;
    },
    // 下拉多选
    openPickerMul(key, data, callback) {
      this.pickerKeyword = ''
      this.callback = callback
      this.currentKey = key;
      this.currentForm = data || this.form;
      this.currentOptions = this.options[key];
      this.currentOptionsCache = this.options[key];
      this.checkBoxMul = this.currentOptions.filter(e => this.currentForm[key].includes(e.value))
        .map(e => e.value);
      this.showPickerMul = true;
    },


    onConfirmDatetimePicker(date) {
      this.currentForm[this.currentKey] = dayjs(date).format(this.dateFormat);
      this.onCancelDatetimePicker()
    },

    onCancelDatetimePicker(){
      this.currentKey = "";
      this.currentForm = null;
      this.showDatetimePicker = false;
    },

    onConfirmCalendar(date) {
      this.currentForm[this.currentKey] = dayjs(date).format(this.dateFormat);
      this.currentKey = "";
      this.currentForm = null;
      this.showCalendar = false;
    },
    onConfirmPicker(data) {
      this.currentForm[this.currentKey + "Name"] = data.label;
      this.currentForm[this.currentKey] = data.value;
      if(this.callback)  this.callback(this.currentForm)
      this.currentKey = "";
      this.currentForm = null;
      this.showPicker = false;
      this.callback = null
    },
    onConfirmPickerMul(data) {
      this.currentForm[this.currentKey + "Name"] = this.currentOptionsCache.filter(e => this.checkBoxMul.includes(e.value))
        .map(e => e.label).join(",");
      this.currentForm[this.currentKey] = this.checkBoxMul.join(",");
      if(this.callback)  this.callback(this.currentForm)
      this.onCancelPickerMul()
    },

    onCancelPickerMul(){
      this.currentKey = "";
      this.currentForm = null;
      this.checkBoxMul = [];
      this.showPickerMul = false;
      this.callback = null
    },

    filterCurrentOptions(){
      this.currentOptions = this.currentOptionsCache.filter(e => e.label.includes(this.pickerKeyword))
    },


    async upload(file) {
      const res = await this.server.upload(file);
      return res.code === "0" ? res : "";
    },

    validate() {
      return new Promise(resolve => {
        this.$refs.form.validate(Object.keys(this.rules)).then(e => {
          resolve(true);
        }).catch((err) => {
          console.error("表单校验未通过：", err);
          resolve(false);
        });
      });
    },

    async submit(url) {
      if(this.loadingSubmit) return
      const bol = await this.validate();
      if (!bol) return this.$toast("请填写表单");
      this.loadingSubmit = true
      this.$toast('提交中，请勿重复提交！')

      const res = await this.server.post(url, this.form);
      if (res.code === "0") {
        this.$toast({ message: "提交成功",duration: 1000,  onClose: () => this.close() });
      }else {
        this.$toast(res.msg || res.message)
        this.loadingSubmit = false
      }
    },
    async save(url) {
      if(this.loadingSubmit) return
      const bol = await this.validate();
      if (!bol) return this.$toast("请填写表单");
      this.loadingSubmit = true
      this.$toast('保存中，请勿重复提交！')
      const res = await this.server.post(url, this.form);
      if (res.code === "0") {
        this.$toast({ message: "保存成功",duration: 1000, onClose: () => this.close() });
      }else {
        this.$toast(res.msg || res.message)
        this.loadingSubmit = false
      }
    },
    close() {
      this.loadingSubmit = false
      window.history.back();
      // if (typeof wx !== "undefined" && wx.miniProgram) {
      //   wx.miniProgram.navigateBack();
      // } else {
      //   window.close();
      // }
    }
  }
};

export { userInfo, form };
